package com.chengliang.bbs.service.impl;

import com.chengliang.bbs.entry.ContinuityPractice;
import com.chengliang.bbs.mapper.ContinuityPracticeMapper;
import com.chengliang.bbs.service.ContinuityPracticeService;
import com.chengliang.bbs.util.StringUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: chengliang
 * @create: 2019-07-31 16:27
 */
@Service
public class ContinuityPracticeServiceImpl implements ContinuityPracticeService {

    @Autowired
    private ContinuityPracticeMapper continuityPracticeMapper;

    @Override
    public ContinuityPractice getContinuityPracticeByUserId(Integer userId) {
        if(StringUtil.isEmpty(userId)){
            return null;
        }
        //通过用户 查询连续签到信息
        ContinuityPractice continuityPractice = continuityPracticeMapper.selectContinuityPracticeByUserId(userId);
        if(continuityPractice == null){
            return null;
        }
        //如果前一天的时间 在最后签到时间 之前 （断签）
        if(LocalDateTime.now().minusDays(1).toLocalDate().isAfter(continuityPractice.getContinuityPracticeLastTime().toLocalDate())){
            continuityPractice.setContinuityPracticeDayNumber(0);
        }
        return continuityPractice;
    }

    @Override
    public List<ContinuityPractice> getHighestPracticeTop20() {
        PageHelper.startPage(1,20);
        return continuityPracticeMapper.selectHighestPracticeTop20();
    }

    @Override
    public List<ContinuityPractice> getNewestPracticeTop20() {
        PageHelper.startPage(1,20);
        return continuityPracticeMapper.selectContinuityPracticeListPracticeTop("desc");
    }

    @Override
    public List<ContinuityPractice> getEarliestPracticeTop20() {
        PageHelper.startPage(1,20);
        return continuityPracticeMapper.selectContinuityPracticeListPracticeTop("asc");
    }
}
